﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Shine.Util.IO;
using System.IO;
using System.Xml;
using Shine.Util.Serialization;

namespace Shine.Util.Configurations
{
    /// <summary>
    /// 系统配置文件操作类
    /// </summary>
    public class ConfigurationHelper
    {
        /// <summary>
        /// 获取指点配置文件中对应节点的值，使用ExeConfigurationFileMap
        /// </summary>
        /// <param name="fullname">*.config配置文件全路径</param>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public static string GetAppSettingValue(string fullname, string key)
        {
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = fullname;
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            if (!config.AppSettings.Settings.AllKeys.Contains(key))
                return "";
            return config.AppSettings.Settings[key].Value;
        }

        /// <summary>
        /// 获取指点配置文件中对应节点的值，使用ConfigurationManager
        /// </summary>
        /// <param name="fullname">*.config配置文件全路径</param>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public static string GetAppSettingValue2(string fullname, string key)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(fullname);
            if (!config.AppSettings.Settings.AllKeys.Contains(key)) return "";
            return config.AppSettings.Settings[key].Value;
        }
        
        /// <summary>
        /// 获取xml配置文件中的对象信息
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="fullname">配置文件全路径，配置文件要存在</param>
        /// <returns></returns>
        public static T GetObjectFromXml<T>(string fullname)
        {
            if (!File.Exists(fullname)) return default(T);
            Type t = typeof(T);
            string nodename = t.Name;
            XmlDocument doc = new XmlDocument();
            doc.Load(fullname);
            XmlNode node = doc.SelectSingleNode(".//" + nodename);
            if (node == null) return default(T);
            return XmlSerialization.FromXml<T>(node.OuterXml);
        }

        /// <summary>
        /// 保存对象到xml配置文件中
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">要保存的对象</param>
        /// <param name="fullname">配置文件全路径，文件若不存在则自动创建</param>
        /// <param name="rootname">根节点的名称</param>
        public static void SaveObjectToXml<T>(T obj, string fullname, string rootname = "Root")
        {
            XmlDocument doc = new XmlDocument();
            if (!File.Exists(fullname)) doc.LoadXml(string.Format("<{0}></{0}>", rootname));
            else doc.Load(fullname);
            string xml = XmlSerialization.ToXml<T>(obj);
            xml = xml.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "").Trim();
            xml = xml.Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "").Trim();
            xml = xml.Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"","").Trim();
            xml = xml.Replace("xmlns=\"http://www.zjgis.com.cn\"", "").Trim();
            XmlDocument tmp = new XmlDocument();
            tmp.LoadXml(xml);
            Type t = typeof(T);
            string nodename = t.Name;
            XmlNode node = doc.SelectSingleNode(".//" + nodename);
            if (node == null)
            {
                node = doc.CreateElement(nodename);
                node.InnerXml = tmp.ChildNodes[0].InnerXml;
                doc.ChildNodes[0].AppendChild(node);
            }
            else
            {
                node.InnerXml = tmp.ChildNodes[0].InnerXml;
            }
            doc.Save(fullname);
        }

    }
}
